package com.tongjie.hotelbooking.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;

@Slf4j
public class HttpUtils {

//    private static PoolingHttpClientConnectionManager connMgr;
//
//    private static RequestConfig requestConfig;

//    private final static int MAX_TIMEOUT = 7000;

//    static {
//        connMgr = new PoolingHttpClientConnectionManager();
//        connMgr.setMaxTotal(100);
//        connMgr.setDefaultMaxPerRoute(connMgr.getMaxTotal());
//        connMgr.setValidateAfterInactivity(1000);
//        RequestConfig.Builder configBuilder = RequestConfig.custom();
//        configBuilder.setConnectTimeout(MAX_TIMEOUT);
//        configBuilder.setSocketTimeout(MAX_TIMEOUT);
//        configBuilder.setConnectionRequestTimeout(MAX_TIMEOUT);
//        requestConfig = configBuilder.build();
//    }


    public static String httpGet(String url, Map<String, String> params) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        return executeHttpGet(url, params, httpClient);
    }

    public static String httpsGet(String url, Map<String, String> params) {
        CloseableHttpClient httpClient = HttpClients
                .custom()
                .setSSLSocketFactory(SSLConnectionSocketFactory.getSocketFactory())
                .setConnectionManager(new PoolingHttpClientConnectionManager())
                .setDefaultRequestConfig(RequestConfig.custom().build()).build();
        return executeHttpGet(url, params, httpClient);
    }

    private static String executeHttpGet(String url, Map<String, String> params, CloseableHttpClient httpClient) {
        CloseableHttpResponse response = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(url);
            for (Map.Entry<String, String> entry : params.entrySet()) {
                uriBuilder.addParameter(entry.getKey(), entry.getValue());
            }
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            response = httpClient.execute(httpGet);
            return response == null ? null : EntityUtils.toString(response.getEntity(), "UTF-8");
        } catch (Exception e) {
            log.error("fail to execute https get request", e);
            return null;
        } finally {
            if (null != response) {
                try {
                    response.close();
                    httpClient.close();
                } catch (IOException e) {
                    log.error("fail to execute https get request", e);
                }
            }
        }
    }

}